<?php
defined('IN_MAIN') or exit('Invalid Access!');

function log_activity($type, $initiator, $iid, $affected_user_type, $affected_uid, $log_code, $message, $extra_fields_value=''){
global $vars;
$db = $vars["db"];
$success = true;
if(is_array($extra_fields_value) && count($extra_fields_value)){
 foreach($extra_fields_value as $field=>$value){
  $value=mysql_real_escape_string($value);
  $formatted_extra_fields.=$field.", ";
  $formatted_extra_field_values.="'".mysql_real_escape_string($value)."', ";
 }
}
if(!mysql_query($sql="insert into $db->activity_log (type, initiator, iid, affected_user_type, affected_uid, code, descr, $formatted_extra_fields cdate) values ('".mysql_real_escape_string($type)."', '".mysql_real_escape_string($initiator)."', '".mysql_real_escape_string($iid)."', '".mysql_real_escape_string($affected_user_type)."', '".mysql_real_escape_string($affected_uid)."', '".mysql_real_escape_string($log_code)."', '".mysql_real_escape_string($message)."', $formatted_extra_field_values'".ndate($vars["system_date_format"])."')")){
 $a_sub="$vars[title] - Error recording activity log";
 $a_msg=
 "<p>Dear admin,</p>
 <p>The system has encountered some problem recording the activity log into the database, as below</p>
 <p>Type: ".$vars["log_code"][$log_code]." ($log_code)<br />\n$message<br />\n---------------------------<br />\nSQL: $sql<br />\n<br />\nError: ".mysql_error()."</p>";
 email_admin($a_sub, $a_msg, 'e');
 $success = false;
}
return $success;
}
?>